package com.example.lcode.binary;


import com.example.lcode.greedy.Question334;

public class Question34 {

    public int[] searchRange(int[] nums, int target) {
           int leftValue = binarySearch(nums, 0, nums.length - 1, Boolean.FALSE,target);
           int rightValue = binarySearch(nums, 0, nums.length - 1, Boolean.TRUE,target);
           return new int[]{leftValue, rightValue};
    }

    private int binarySearch(int[] nums, int left, int right, Boolean sign, int target) {
        if (left > right) {
            return -1;
        }
        int mid = (left + right) / 2;
        if (nums[mid] == target
                && ((!sign && (mid == 0 || nums[mid - 1] < nums[mid])) ||(sign && (mid == right || nums[mid + 1] > nums[mid])))) {
            return mid;
        } else if (!sign && nums[mid] >= target || sign && nums[mid] > target) {
            return binarySearch(nums, 0, mid - 1, sign, target);
        } else {
            return binarySearch(nums, mid + 1, right, sign, target);
        }
    }

    public static void main(String[] args) {
        Question34 question33 = new Question34();
        int []arr = {3,3,3};
        question33.searchRange(arr, 3);
    }

}
